package com.lh.music.fragment.discover;


import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lh.music.R;
import com.lh.music.activity.DiantaiDetailActivity;
import com.lh.music.activity.MoreDiantaiDetailActivity;
import com.lh.music.adapter.GridViewAdapter;
import com.lh.music.api.Api;
import com.lh.music.callback.CallBack;
import com.lh.music.entity.Diantai;
import com.lh.music.util.RequestUtil;
import com.lh.music.view.MyGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiantaiFragment extends BaseFragment implements CallBack{
    private View view;
    private LinearLayout container;
    private RequestUtil requestUtil;
    private List<Diantai> diantaiList=new ArrayList<>();
    private List<Diantai> tuiJianList=new ArrayList<>();
    private List<Diantai> shiGuangList=new ArrayList<>();
    private List<Diantai> fengGeList=new ArrayList<>();
    private List<Diantai> xinQingList=new ArrayList<>();
    private List<Diantai> yuZhongList=new ArrayList<>();
    private GridViewAdapter adapter;
    private boolean isFirst=true;
    public DiantaiFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (view==null){
            view=inflater.inflate(R.layout.fragment_zhubo, container, false);
            requestUtil=new RequestUtil(getActivity(),this);
            isPrepare=true;
            initLoadView();
            lazyLoad();
        }

        return view;
    }

    @Override
    protected void lazyLoad() {
        if (isVisiable&&isPrepare&&isFirst){
            requestUtil.getPaihangContent(Api.DIANTAI_API);
            isFirst=false;
        }
    }

    @Override
    public void callBack(String json) {
        //网络访问完毕，将正在加载隐藏掉
        hiddenLoad();
        JSONObject job= JSON.parseObject(json);
        JSONArray array=job.getJSONArray("result");
        JSONObject result=array.getJSONObject(0);
        JSONArray channellist=result.getJSONArray("channellist");
        diantaiList.addAll(JSON.parseArray(channellist.toJSONString(),Diantai.class));
        //将电台的总数据，分发给对应的频道
        sendData(diantaiList);
        //将对应的数据绑定给对应的gridview
        bindDataToGridView(tuiJianList);
        bindDataToGridView(shiGuangList);
        bindDataToGridView(fengGeList);
        bindDataToGridView(xinQingList);
        bindDataToGridView(yuZhongList);
    }



    @Override
    public void errorCallBack() {
        hiddenLoad();
    }
    private void sendData(List<Diantai> diantaiList) {
        for (int i=0;i<diantaiList.size();i++){
            Diantai d=diantaiList.get(i);
            switch (d.getCate_sname()){
                case "推荐频道":
                    tuiJianList.add(d);
                    break;
                case "时光频道":
                    shiGuangList.add(d);
                    break;
                case "风格频道":
                    fengGeList.add(d);
                    break;
                case "心情频道":
                    xinQingList.add(d);
                    break;
                case "语种频道":
                    yuZhongList.add(d);
                    break;
            }
        }
    }
    //将对应的数据绑定给对应的gridview
    private void bindDataToGridView(final List<Diantai> list){
        container= (LinearLayout) view.findViewById(R.id.container);
        View child=LayoutInflater.from(getActivity())
                .inflate(R.layout.mygridview,null);
        TextView type= (TextView) child.findViewById(R.id.type);
        MyGridView gridView= (MyGridView) child.findViewById(R.id.gridView);

        //单击更多跳到更多电台详情页
        child.findViewById(R.id.more).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<Diantai> arrayList= (ArrayList<Diantai>) list;
                Intent intent=new Intent(getActivity(), MoreDiantaiDetailActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("diantaiList",arrayList);
                intent.putExtra("bundle",bundle);
                startActivity(intent);
            }
        });
        Diantai d=list.get(0);
        switch (d.getCate_sname()){
            case "推荐频道":
                type.setText("推荐频道");
                gridView.setNumColumns(3);
                adapter=new GridViewAdapter(getActivity(),list,6);
                gridView.setAdapter(adapter);
                container.addView(child);
                break;
            case "时光频道":
                type.setText("时光频道");
                gridView.setNumColumns(2);
                adapter=new GridViewAdapter(getActivity(),list,4);
                gridView.setAdapter(adapter);
                container.addView(child);
                break;
            case "风格频道":
                type.setText("风格频道");
                gridView.setNumColumns(3);
                adapter=new GridViewAdapter(getActivity(),list,3);
                gridView.setAdapter(adapter);
                container.addView(child);
                break;
            case "心情频道":
                type.setText("心情频道");
                gridView.setNumColumns(2);
                adapter=new GridViewAdapter(getActivity(),list,4);
                gridView.setAdapter(adapter);
                container.addView(child);
                break;
            case "语种频道":
                type.setText("语种频道");
                gridView.setNumColumns(3);
                adapter=new GridViewAdapter(getActivity(),list,3);
                gridView.setAdapter(adapter);
                container.addView(child);
                break;
             }
        //为gridview添加监听事件
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getActivity(), DiantaiDetailActivity.class);
                intent.putExtra("channelName",list.get(position).getCh_name());
                startActivity(intent);
            }
        });
    }
    /**
     * 正在加载的初始化
     */
    public void initLoadView(){
        //baseView=LayoutInflater.from(context).inflate(R.layout.fragment_baseragment, null, false);
        ImageView iv= (ImageView) view.findViewById(R.id.load);
        AnimationDrawable anim= (AnimationDrawable) getActivity().getResources().getDrawable(R.drawable.loading_anim);
        iv.setBackground(anim);
        anim.start();
    }
    public void hiddenLoad(){
        view.findViewById(R.id.load_container).setVisibility(View.GONE);
    }
}
